import sys

import pytest
import os
import pandas
import logging
from Logger.Logger import set_logger

def run_test():
    # 获取当前的脚本所在的目录
    # 绝对路径
    current_dir=os.path.dirname(os.path.abspath(__file__))
    sys.path.append(os.path.dirname(current_dir))

    # 获取日志
    logger=set_logger()

    # 设置allure报告保存的目录
    allure_report_dir=os.path.join(current_dir,'Reports','allure_reports')
    if not os.path.isdir(allure_report_dir):
        os.mkdir(allure_report_dir)
    # 设置html报告的保存路径
    html_report_dir=os.path.join(current_dir,'Reports','html_reports')
    if not os.path.isdir(html_report_dir):
        os.mkdir(html_report_dir)
    # html报告文件路径
    html_report_path=os.path.join(html_report_dir,'test_report.html')\
    # 添加异常处理
    try:
        logger.info("开始运行用例")
        pytest.main([
            "-s","-v",
            "--alluredir="+allure_report_dir,
            "--html="+html_report_path,
            "--self-contained-html",
            "--clean-alluredir"
        ])
    except Exception as e:
        logger.error(f"生成报告错误{str(e)}")

if __name__ == '__main__':
    run_test()
    os.system("allure serve Reports/html")

